﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using wp7cinema.MaxmovieWebservice;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;
using wp7cinema.Model;
using GalaSoft.MvvmLight.Command;

namespace wp7cinema.ViewModel
{
    public class TheaterViewModel : ViewModelBase
    {
        public string ApplicationTitle { get { return "Cinema Search"; } }
        public string PageName { get { return "극장 찾기"; } }

        private Dictionary<string, ObservableCollection<TheaterSimpleProperty>> allTheaters;

        public ObservableCollection<TheaterSimpleProperty> _theaters;
        public ObservableCollection<TheaterSimpleProperty> Theaters
        {
            get { return _theaters; }
            set
            {
                _theaters = value;
                RaisePropertyChanged("Theaters");
            }
        }

        public IEnumerable<Region> _regionList;
        public IEnumerable<Region> RegionList
        {
            get { return _regionList; }
            set
            {
                _regionList = value;
                RaisePropertyChanged("RegionList");
            }
        }

        WS_ReserveSoapClient wsClient;

        public RelayCommand<Region> RegionChangeCommand { get; set; }
        public void RegionChangeCommandExecute(Region region)
        {
            if (allTheaters.ContainsKey(region.Code))
                Theaters = allTheaters[region.Code];
            else
            {
                Theaters.Clear();
                Theaters.Add(new TheaterSimpleProperty { TNAME = "극장없음." });
            }
        }

        public TheaterViewModel()
        {
            RegionChangeCommand = new RelayCommand<Region>(RegionChangeCommandExecute);

            if (IsInDesignMode)
            {
                ObservableCollection<Region> r = new ObservableCollection<Region>();
                for (int i = 0; i < 10; i++)
                {
                    r.Add(new Region { Name = "지역 " + i, Code = "000" + i });
                }
                RegionList = r;
                ObservableCollection<TheaterSimpleProperty> t = new ObservableCollection<TheaterSimpleProperty>();
                for (int i = 0; i < 10; i++)
                {
                    t.Add(new TheaterSimpleProperty { TNAME = "극장 " + i });
                }
                Theaters = t;
            }
            else
            {
                wsClient = new WS_ReserveSoapClient();

                wsClient.getRegionListCompleted += new EventHandler<getRegionListCompletedEventArgs>(wsClient_getRegionListCompleted);
                wsClient.getRegionListAsync();

                wsClient.getTheaterSimpleListAllCompleted += new EventHandler<getTheaterSimpleListAllCompletedEventArgs>(wsClient_getTheaterSimpleListAllCompleted);
                wsClient.getTheaterSimpleListAllAsync("", "");
            }
        }

        void wsClient_getRegionListCompleted(object sender, getRegionListCompletedEventArgs e)
        {
            XElement element = e.Result.Nodes[0];
            RegionList = element.Descendants("DATAROWS")
                .Select(row => new Region
                {
                    Code = row.Element("REGION_CODE").Value,
                    Name = row.Element("REGION").Value
                });
        }

        void wsClient_getTheaterSimpleListAllCompleted(object sender, getTheaterSimpleListAllCompletedEventArgs e)
        {
            allTheaters = new Dictionary<string, ObservableCollection<TheaterSimpleProperty>>();
            foreach (TheaterSimpleProperty t in e.Result)
            {
                if (!allTheaters.ContainsKey(t.REGION_CODE))
                {
                    allTheaters.Add(t.REGION_CODE, new ObservableCollection<TheaterSimpleProperty>());
                }
                allTheaters[t.REGION_CODE].Add(t);
            }
        }
    }
}
